//
//  CZMainViewController.swift
//  WeiBo
//
//  Created by Apple on 16/9/13.
//  Copyright © 2016年 KY. All rights reserved.
//

import UIKit

//  根视图控制器
class CZMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        //  添加子控制器的方式
//        viewControllers
//        addChildViewController(xx)
        
        //  全局统一设置tabbr的tintColor, 越早执行越好，一般会放到AppDelegate里面
//        UITabBar.appearance().tintColor = UIColor.orange
        
        //  设置自定义的tabbar
        let czTabBar = CZTabBar()
        //  设置代理对象
        czTabBar.czDelegate = self
        //  设置闭包， 如果使用self会产生循环引用
        czTabBar.composeButtonClosure = { [weak self] in
            print("我是闭包回调过来的")
        }
        
        //  kvc给私有属性进行初始化
        setValue(czTabBar, forKey: "tabBar")
        
        
        addChildViewController(childController: CZHomeTableViewController(), imageName: "tabbar_home", title: "首页")
        
        addChildViewController(childController: CZMessageTableViewController(), imageName: "tabbar_message_center", title: "消息")
        
        addChildViewController(childController: CZDisCoverTableViewController(), imageName: "tabbar_discover", title: "发现")
        
        addChildViewController(childController: CZProfileTableViewController(), imageName: "tabbar_profile", title: "我的")
    }

    //  重载添加子控制器的方法
    func addChildViewController(childController: UIViewController, imageName: String, title: String) {
        
        //  设置tabbar的图片
        childController.tabBarItem.image = UIImage(named: imageName)
        //  选中图片
//        childController.tabBarItem.selectedImage = UIImage(named: "\(imageName)_highlighted")
        childController.tabBarItem.selectedImage = UIImage(named: "\(imageName)_selected")?.withRenderingMode(.alwaysOriginal)
        //  设置标题选中颜色
        childController.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orange], for: .selected)
        //  字体大小
        childController.tabBarItem.setTitleTextAttributes([NSFontAttributeName: UIFont.systemFont(ofSize: 13)], for: .normal)
        
        //  tabaritem的标题
//        childController.tabBarItem.title = title
//        childController.navigationItem.title = title
        //  合并一句代码
        childController.title = title
        
        //  创建导航控制器
        let nav = CZNavigationController(rootViewController: childController)
        //  添加子控制器
        addChildViewController(nav)
        
        
    }
    
    

}

extension CZMainViewController: CZTabBarDelegate {
    func didSelectedComposeButton() {
        print("我是代理对象调用过来的")
    }
}















